package com.ruoyi.mongo.souce;

import org.bson.Document;

// import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;

/**
 * @Description: 获取mongo链接 
 * @ClassName: MongoDB01Util
 * @author syc
 * @date: 2022年5月25日 上午10:23:20
 */
public class MongoDB01Util {
//    private static  MongoClient client = null;
//    static{
//        if(client == null) {
//            client = new MongoClient("127.0.0.1",27017);
//        }
//    }

    // 获取MongoDB数据库
    public static MongoDatabase getDatabase(String dbName) {
       //return client.getDatabase(dbName);
       return null;
    }

    // 获取MongiDb中的集合
    public static MongoCollection<Document> getCollection(String dbName ,String collName) {
        MongoDatabase database = getDatabase(dbName);
        return database.getCollection(collName);
    }

    /**
     * @Description:测试 
     * @author syc
     * @date: 2022年5月25日 上午10:27:05
     */
    public static void main(String[] args) {

    	MongoCollection<Document>	collection = MongoDB01Util.getCollection("user_test", "student");
    	 FindIterable<Document> iterable = collection.find();
         MongoCursor<Document> cursor = iterable.iterator();
         while(cursor.hasNext()){
            // 每掉一次next移动以下指针
            Document docu = cursor.next();
            System.out.println(docu.get("name")+"\t" + docu.get("age") + "\t" + docu.get("birthday") + "\t" + docu.get("heigth"));
         }
	}
    
    
    
}
